# /usr/bin/env python3.6
# -*- encoding: utf-8 -*-
"""
@File    : cinema_auth_api.py
@Time    : 2020/2/7 0007 17:07
@Author  : zhangbing
@Email   : 18829172841@163.com
@Software: PyCharm
"""

'''
此模块主要用于管理员修改和查询影院设置的相关配置，其可修改影院用户的状态，查看影院用户的状态和列表等操作
'''
from flask_restful import reqparse, Resource, marshal, fields, abort

from App.apis.admin.utils import login_admin_required
from App.apis.api_constant import HTTP_OK
from App.models.cinema_admin.cinema_user_model import CinemaUser
from App.models.cinema_admin.model_utils import get_cinema_user

cinema_user_fields = {
    "username": fields.String,
    "phone": fields.String,
    "password": fields.String(attribute="_password"),
    "is_verify": fields.Boolean,
}

parse = reqparse.RequestParser()

parse.add_argument("is_verify", type=int, required=True, help="请提供相关修改操作！")


class AdminCinemaUserResource(Resource):
    @login_admin_required
    def get(self, id):
        cinema_user = get_cinema_user(id)
        if not cinema_user:
            abort(404, msg="该用户不存在")
        data = {
            "msg": "ok",
            "status": HTTP_OK,
            "data": marshal(cinema_user, cinema_user_fields)
        }
        return data

    @login_admin_required
    def patch(self, id):
        args = parse.parse_args()
        is_verify = args.get("is_verify")
        cinema_user = CinemaUser.query.get(id)
        cinema_user.is_verify = is_verify

        if not cinema_user.save():
            abort(400, msg="change  fail")

        data = {
            "msg": "ok",
            "status": HTTP_OK,
            "data": marshal(cinema_user, cinema_user_fields)
        }
        return data


class AdminCinemaUsersResource(Resource):
    @login_admin_required
    def get(self):
        cinema_users = CinemaUser.query.all()
        data = {
            "msg": "ok",
            "status": HTTP_OK,
            "data": marshal(cinema_users, cinema_user_fields)
        }
        return data


if __name__ == "__main__":
    pass
